Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
resolve-package-path
Advanced tools
a special purpose fast memoizing way to resolve a node modules package.json
The resolve-package-path npm package is used to resolve the path of a package in the node_modules directory. It helps in finding the exact location of a package, which can be useful for various purposes such as loading configurations, plugins, or dependencies dynamically.
Resolve package path
This feature allows you to resolve the path of a given package from the node_modules directory. In this example, it resolves the path of the 'express' package from the current working directory.
const resolvePackagePath = require('resolve-package-path');
const packagePath = resolvePackagePath('express', process.cwd());
console.log(packagePath);
Resolve package path with custom base directory
This feature allows you to resolve the path of a package from a custom base directory. In this example, it resolves the path of the 'express' package from a specified custom base directory.
const resolvePackagePath = require('resolve-package-path');
const packagePath = resolvePackagePath('express', '/path/to/custom/base/directory');
console.log(packagePath);
Resolve package path with cache
This feature allows you to resolve the path of a package with caching. The cache helps in improving performance by storing previously resolved paths. In this example, it resolves the path of the 'express' package with a cache.
const resolvePackagePath = require('resolve-package-path');
const cache = new Map();
const packagePath = resolvePackagePath('express', process.cwd(), cache);
console.log(packagePath);
The 'resolve' package is used to resolve the path of a module like Node's require.resolve() but with more features. It can resolve paths based on custom file extensions, directories, and more. Compared to resolve-package-path, 'resolve' offers more flexibility and options for resolving module paths.
The 'pkg-dir' package finds the root directory of a Node.js project or a package. It is useful for locating the root directory of a project or a package, which can then be used to resolve paths relative to that root. While resolve-package-path focuses on resolving the path of a specific package, 'pkg-dir' helps in finding the root directory of a project or package.
The 'find-up' package is used to find a file or directory by walking up parent directories. It is useful for locating configuration files or other resources that may be located in parent directories. Compared to resolve-package-path, 'find-up' is more general-purpose and can be used to find any file or directory, not just package paths.
This project is special-purpose, made to resolve a package.json file given a specific module name and basedir to start searching from. It cannot and does not resolve anything else.
To achieve its file-resolution performance, it does two specific things:
It memoizes results identically to node's require
. Specifically,
for a given moduleName and baseDir it will, for the duration of the process,
always return the exact same response.
It re-implements the parts of require.resolve
needed to resolve package.json
files ONLY. This removes unneeded I/O. (based on @davecombs approach)
yarn add resolve-package-path
const resolvePackagePath = require('resolve-package-path');
resolvePackagePath('rsvp', 'base-dir/to/start/the/node_resolution-algorithm-from') => // /path/to/rsvp.json or null
Node supports --preserve-symlinks
and NODE_PRESERVE_SYMLINKS=1
for compatibility this library respects these.
Although by default resolve-package-path
caches or memoizes results, this feature can be disabled:
const resolvePackagePath = require('resolve-package-path');
resolvePackagePath('rsvp', 'base-dir/to/start/the/node_resolution-algorithm-from', false) => // uncached result /path/to/rsvp.json or null
const resolvePackagePath = require('resolve-package-path');
resolvePackagePath._resetCache();
In some advanced circumtances, you may want to gain access to the cache to share between more systems. In that case, a cache instance of the following form can be provided as a third argument:
cache = {
RESOLVED_PACKAGE_PATH: new Map(),
REAL_FILE_PATH: new Map(),
REAL_DIRECTORY_PATH: new Map()
};
const resolvePackagePath = require('resolve-package-path');
resolvePackagePath('rsvp', 'path/to/start/from', cache);
For consumers who also do getRealFilePath
or
getRealDirectoryPath
calls on relevant paths, we expose them as utilities.
These utilties ensure identical functionality to resolve-package-path, and a
shared cache, which may help reduce IO.
FAQs
a special purpose fast memoizing way to resolve a node modules package.json
The npm package resolve-package-path receives a total of 993,789 weekly downloads. As such, resolve-package-path popularity was classified as popular.
We found that resolve-package-path demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.